package com.aaa.movie.controller;

import com.aaa.movie.entity.FilmInfo;
import com.aaa.movie.service.FilmInfoService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (FilmInfo)表控制层
 *
 * @author teacherChen
 * @since 2025-02-17 11:11:28
 */
@RestController
@RequestMapping("filmInfo")
public class FilmInfoController {
    /**
     * 服务对象
     */
    @Resource
    private FilmInfoService filmInfoService;

    /**
     * 分页查询
     *
     * @param filmInfo 筛选条件
     * @param page      当前第几页
     * @param limit      一页显示多少条
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<FilmInfo>> queryByPage(FilmInfo filmInfo, Integer page,Integer limit) {
        PageRequest pageRequest = PageRequest.of(page-1, limit, Sort.unsorted());
        return ResponseEntity.ok(this.filmInfoService.queryByPage(filmInfo, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<FilmInfo> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.filmInfoService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param filmInfo 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<FilmInfo> add(FilmInfo filmInfo) {
        return ResponseEntity.ok(this.filmInfoService.insert(filmInfo));
    }

    /**
     * 编辑数据
     *
     * @param filmInfo 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<FilmInfo> edit(FilmInfo filmInfo) {
        return ResponseEntity.ok(this.filmInfoService.update(filmInfo));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        System.out.println("11111111111");
        return ResponseEntity.ok(this.filmInfoService.deleteById(id));
    }

}

